
capture log close

set more off
set matsize 10000

use rep_final, clear

	*noncitizens
	keep if cit==0
	*married last year, currently still married with spouse present
	keep if marrinyr==2 & married==1 
	*spouse info is avail.
	drop if citizen_sp==.

* age of marriage

	g agemarried= age if yrmarr==year
	replace agemarried= age - 1 if yrmarr<year

* Spouse age of marriage
	g agemarried_sp = age_sp if yrmarr==year
	replace agemarried_sp = age_sp - 1 if yrmarr<year
	
* Define control variables 

	local demog "age age2 male yreduc i.racegr"
	
	local demog1 "yreduc i.racegr"
	local demog2 "age age2 i.racegr"
	
	local imm "ysm i.bpl " 
	local fe "i.statefip i.yrmarr "
	
	
	local full "`demog' `imm' `fe' "
	local full1 "`demog1' `imm' `fe'"
	local full2 "`demog2' `imm' `fe'"
	
	g post1=(yrmarr>=2013 & yrmarr<=2016)
	g post2=(yrmarr>=2017)

	
* age,educ, emp of daca and their spouses with citizen spouse, 
* compared to control group, by gender


	g dacacitsp=treatgr*cit_sp
	g citsppost1=cit_sp*post1
	g citsppost2=cit_sp*post2
	g treatpost1=treatgr*post1
	g treatpost2=treatgr*post2
	g treatcitsppost1=treatgr*cit_sp*post1
	g treatcitsppost2=treatgr*cit_sp*post2


 	foreach gender of varlist male female{
	
	preserve
		keep if `gender'==1
		

			*citizen vs noncit spouse, daca vs. control group, before and after, post2012 and post2016
			reg agemarried treatgr cit_sp dacacitsp citsppost1 citsppost2 treatpost1 treatpost2 ///
				treatcitsppost1 treatcitsppost2 ///
				`full1' if daca==1|controlgrdaca==1 [pweight = perwt] , cluster(statefip)
			est sto b1	
				*dep var mean
				sum agemarried [fweight=perwt] if e(sample) 
			
			reg yreduc agemarried treatgr cit_sp dacacitsp citsppost1 citsppost2 treatpost1 treatpost2 ///
				treatcitsppost1 treatcitsppost2 ///
				`full2' if daca==1|controlgrdaca==1 [pweight = perwt] , cluster(statefip)
			est sto b2
				*dep var mean
				sum yreduc [fweight=perwt] if e(sample) 
			
			reg emp agemarried treatgr cit_sp dacacitsp citsppost1 citsppost2 treatpost1 treatpost2 ///
				treatcitsppost1 treatcitsppost2 ///
				`full' if daca==1|controlgrdaca==1  [pweight = perwt] , cluster(statefip)
			est sto b3
				*dep var mean
				sum emp [fweight=perwt] if e(sample) 	
				
			reg agemarried_sp agemarried treatgr cit_sp dacacitsp citsppost1 citsppost2 treatpost1 treatpost2 ///
				treatcitsppost1 treatcitsppost2 ///
				`full1' if daca==1|controlgrdaca==1  [pweight = perwt] , cluster(statefip)
			est sto b4	
				*dep var mean
				sum agemarried_sp [fweight=perwt] if e(sample) 
				
			reg yreduc_sp agemarried treatgr cit_sp dacacitsp citsppost1 citsppost2 treatpost1 treatpost2 ///
				treatcitsppost1 treatcitsppost2 ///
				`full2' if daca==1|controlgrdaca==1 [pweight = perwt] , cluster(statefip)
			est sto b5
				*dep var mean
				sum yreduc_sp [fweight=perwt] if e(sample) 	

			reg emp_sp agemarried treatgr cit_sp dacacitsp citsppost1 citsppost2 treatpost1 treatpost2 ///
				treatcitsppost1 treatcitsppost2 ///
				`full' if daca==1|controlgrdaca==1  [pweight = perwt] , cluster(statefip)
			est sto b6
				*dep var mean
				sum emp_sp [fweight=perwt] if e(sample) 
			
	
			*Make table 
			*daca vs control group, with citizen spouse
			esttab b1 b2 b3 b4 b5 b6   ///
					using table7_`gender'.csv, se  ///
					title(Intermarriage Rate) ///
					b(%9.4f) se(%9.4f) star(* 0.1 ** 0.05 *** 0.01) ///
					nogaps replace
	
		
	restore	
	}
